﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Cmd;
using GameData;
using UnityEngine.UI;

public class DueUI : MonoBehaviour
{

    public List<WinView> TextList = new List<WinView>();

    public GameObject backBtn;

    public AudioClip audio_qing;
    public List<AudioClip> AudioClips = new List<AudioClip>();
    public List<AudioClip> EnAudioClips = new List<AudioClip>(); 


    public AudioClip ptYwt;

    public List<AudioClip> AudioClips2 = new List<AudioClip>();
    public List<AudioClip> EnAudioClips2 = new List<AudioClip>();

    public AudioClip yyYwt;

    public Queue<CallNumber>  CallNumbers = new Queue<CallNumber>();

    public bool iscomplete = true;

    public AudioSource AudioSource;
    public int indx = 3;
    public CallNumber CurCallNumber;
    public string soutUrl;
    public Queue<AudioClip> CallAudioClips = new Queue<AudioClip>();
    public SoundType SoundType;
    public bool isonlyOne=true;
    // Use this for initialization
    void Start () {

        Messenger<DueNuminfos>.AddListenerByOne("SC处理名单",onDueResponse);
        Messenger<CallNumber>.AddListenerByOne("SC叫号", OnCallNumer);
        Messenger<SoundType>.AddListenerByOne("SC设置语种", SetSoundType);

        EventTriggerListener.Get(backBtn).onClick = OnBack;

        DueNumberListSend duesend = new DueNumberListSend();
        duesend.Send(GlobalVO.GetInstance().roleInfo.UserInfo, onDueResponse);
        SoundType = GlobalVO.GetInstance().SoundType;
        indx = 3;
        //
        soutUrl = "Sound/";
        GetSoundType getSoundType = new GetSoundType();
        getSoundType.Send(GlobalVO.GetInstance().roleInfo.UserInfo, SetSoundType);
    }

    public List<AudioClip> UseEnAudioClips;

    public void SetSoundType(SoundType soundType)
    {
        GlobalVO.GetInstance().SoundType = soundType;
        SoundType = soundType;
        if (soundType.putonghua)
        {
            soutUrl = "Sound/";
            UseEnAudioClips = EnAudioClips;
        }
        else if (soundType.yueyu)
        {

            soutUrl = "Sound2/";
            UseEnAudioClips = EnAudioClips2;
        }
        audio_qing = Resources.Load<AudioClip>(soutUrl + "qing");

    }





    void OnCallNumer(CallNumber callNumber)
    {
        Debug.Log(callNumber.windowsid+" 呼叫号码：" + callNumber.numberInfo.value);
        int state = 0; //1删除 //2覆盖
        foreach (var number in CallNumbers)
        {
            if (number.windowsid == callNumber.windowsid && number.numberInfo.value == callNumber.numberInfo.value)
            {
                state = 1;
                break;
            }
            else if (number.windowsid == callNumber.windowsid && number.numberInfo.value != callNumber.numberInfo.value)
            {
                number.isRead = false;
                state = 2;
                break;
            }
        }
        //
        Debug.Log("state: " + state + "呼叫数量:" + CallNumbers.Count+" "+ iscomplete);

        if (state!=1)
        {
            CallNumbers.Enqueue(callNumber);
        }
        Calling();

    }

    //取号从 001  002 读法 3位数
    Queue<AudioClip> intToClip2(int num)
    {
        Queue<AudioClip> res = new Queue<AudioClip>();
        string numstr = UtilTool.getNumbertoStr(num);
        for (int i = 0; i < numstr.Length; i++)
        {
            string numsfin = numstr[i].ToString();
            AudioClip ac = Resources.Load<AudioClip>(soutUrl + numsfin);
            res.Enqueue(ac);
        }
        return res;

    }


    Queue<AudioClip> intToClip(int num)
    {
        Queue<AudioClip> res = new Queue<AudioClip>();


        int qsx = num/1000;
        if (qsx!=0)
        {
            string finds = qsx + "000";
            AudioClip ac = Resources.Load<AudioClip>(soutUrl + finds);
            res.Enqueue(ac);
        }

        int dsx = (num-qsx*1000)/100;
        if (dsx!=0)
        {
            string finds = dsx + "00";
            AudioClip ac = Resources.Load<AudioClip>(soutUrl + finds);
            res.Enqueue(ac);
        }
        int sx = (num - qsx*1000-dsx*100)/10;
        if (sx!=0)
        {
            string finds = sx + "0";
            AudioClip ac = Resources.Load<AudioClip>(soutUrl + finds);
            res.Enqueue(ac);
        }
        int x = (num - dsx * 100-sx*10) ;
        if (x != 0)
        {
            string finds = x.ToString() ;
            AudioClip ac = Resources.Load<AudioClip>(soutUrl + finds);
            res.Enqueue(ac);
        }


        return res;
    } 

    void Calling()
    {

        if (iscomplete )
        {
            if (CallNumbers.Count<=0)
            {
                return;
                
            }
            CurCallNumber = CallNumbers.Dequeue();
            CallAudioClips.Enqueue(audio_qing);
            //字母
            //if (CurCallNumber.numberInfo.groupTag=="A")
            //{
            //    CallAudioClips.Enqueue(UseEnAudioClips[0]);
            //}
            //else if (CurCallNumber.numberInfo.groupTag == "B")
            //{
            //    CallAudioClips.Enqueue(UseEnAudioClips[1]);
            //}
            //else if (CurCallNumber.numberInfo.groupTag == "C")
            //{
            //    CallAudioClips.Enqueue(UseEnAudioClips[2]);
            //}
            //else if (CurCallNumber.numberInfo.groupTag == "D")
            //{
            //    CallAudioClips.Enqueue(UseEnAudioClips[3]);
            //}

            Queue<AudioClip>   NumAudioClips = intToClip2(CurCallNumber.numberInfo.value);
            int count = NumAudioClips.Count;
            for (int i = 0; i < count; i++)
            {
                CallAudioClips.Enqueue(NumAudioClips.Dequeue());
            }
            if (SoundType.putonghua)
            {
                if (isonlyOne)
                {
                    CallAudioClips.Enqueue(ptYwt);
                }
                else
                {
                    CallAudioClips.Enqueue(AudioClips[CurCallNumber.windowsid - 1]);

                }
            }
            else if (SoundType.yueyu)
            {
                if (isonlyOne)
                {
                    CallAudioClips.Enqueue(yyYwt);
                }
                else
                {
                    CallAudioClips.Enqueue(AudioClips2[CurCallNumber.windowsid - 1]);

                }
            }

            iscomplete = false;
            PlaySound();
        }


    }


    void PlaySound()
    {
        AudioSource.Stop();
        AudioSource.clip = CallAudioClips.Dequeue();
        AudioSource.Play();

    }

    // Update is called once per frame
    void Update()
    {
        if (!iscomplete)
        {
            if (CallAudioClips.Count<=0)
            {
                if (!AudioSource.isPlaying)
                {
                    iscomplete = true;
                    Calling();
                }

            }
            else
            {
                if (!AudioSource.isPlaying)
                {
                    AudioSource.Stop();
                    AudioSource.clip = CallAudioClips.Dequeue();
                    AudioSource.Play();
                }

            }
        }
    }



    void OnBack(GameObject obj)
    {
        OnRemoveListener();
        Application.LoadLevel("Main");

    }

    public void OnRemoveListener()
    {
        Messenger<DueNuminfos>.RemoveListener("SC处理名单", onDueResponse);
        Messenger<CallNumber>.RemoveListener("SC叫号", OnCallNumer);
        Messenger<SoundType>.RemoveListener("SC设置语种", SetSoundType);

    }


    void onDueResponse(DueNuminfos dueNuminfos)
    {
        List<int> nolist =new List<int>();
        int vcount = dueNuminfos.NumberInfoList.Count;
        for (int i = 0; i < vcount; i++)
        {
            NumberInfo numberInfo = dueNuminfos.NumberInfoList[i];
           
            WinView winView = null;
            for (int j = 0; j < TextList.Count; j++)
            {
                WinView temp = TextList[j];
                if (temp.index == numberInfo.WindowNum)
                {
                    winView = temp;
                    nolist.Add(j);
                    break;
                }

            }
            //
            if (winView!= null)
            {
                winView.gameObject.SetActive(true);
                //
                //winView.Text.text = "请" + numberInfo.value + "号到" + winView.index + "号业务台办理";
                winView.Text.text = "请" + UtilTool.getNumbertoStr(numberInfo.value) + "号到销控区办理";

            }

        }
        //
        for (int i = 0; i < TextList.Count; i++)
        {
            if (!nolist.Contains(i))
            {
                WinView temp = TextList[i];
                temp.Text.text = "";

            }
        }

    }


}
